//
//  CellOfLike.m
//  project-A (pianke)
//
//  Created by dllo on 16/2/1.
//  Copyright © 2016年 scott. All rights reserved.
//

#import "CellOfLike.h"
#import "CellLikeCollect.h"
#import "ModelOfLike.h"

#import <UIImageView+WebCache.h>
#import <MJRefresh.h>
#import <AFNetworking.h>
#import <DKNightVersion.h>
#define WIDTH    [UIScreen mainScreen].bounds.size.width

#define HEIGHT    [UIScreen mainScreen].bounds.size.height

@interface CellOfLike ()<UICollectionViewDataSource,UICollectionViewDelegate>

@property(nonatomic,retain)NSMutableArray *arr;

@property(nonatomic,assign)NSInteger number;


@end


@implementation CellOfLike

-(void)dealloc{
    [_collection release];
    [_arr release];
    [super dealloc];
}

-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.number = 2;
        
        [self creatcollection];
        
    }
    
    
    return self;
}

-(void)creatcollection{
   
    UICollectionViewFlowLayout *flowlaout = [[UICollectionViewFlowLayout alloc]init];
    flowlaout.itemSize = CGSizeMake((WIDTH - 15)/3, (WIDTH - 15)/3);
    flowlaout.scrollDirection = UICollectionViewScrollDirectionVertical;
    flowlaout.minimumInteritemSpacing =2.5;
    flowlaout.minimumLineSpacing = 2.5;
    
    flowlaout.footerReferenceSize = CGSizeMake(WIDTH, 10);
    
    
    
    self.collection = [[UICollectionView alloc]initWithFrame:CGRectMake(5, 5, WIDTH - 10, HEIGHT - 212)collectionViewLayout:flowlaout];
    [self addSubview:self.collection];
    
    self.collection.dataSource = self;
    self.collection.delegate = self;
    
    self.collection.pagingEnabled = YES;
    self.collection.backgroundColor = [UIColor whiteColor];
    self.collection.bounces = NO;
   
    
    self.collection.dk_backgroundColorPicker  = DKColorWithColors([UIColor whiteColor], [UIColor colorWithRed:0.64 green:0.74 blue:0.86 alpha:1]);
    
    
    [self.collection registerClass:[CellLikeCollect class] forCellWithReuseIdentifier:@"pool1"];
    
    [self.collection registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"poolForFooter"];
    
    [flowlaout release];
    [self.collection release];
    
}

-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    
    
        UICollectionReusableView *foot = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"poolForFooter" forIndexPath:indexPath];
        
        foot.backgroundColor = [UIColor whiteColor];
       
    
    
      return foot;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return self.arr.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    CellLikeCollect *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"pool1" forIndexPath:indexPath];
    
    ModelOfLike *model = [self.arr objectAtIndex:indexPath.row];
    
    
    [cell.image sd_setImageWithURL:[NSURL URLWithString:model.photo_url] placeholderImage:nil options:SDWebImageAvoidAutoSetImage];
    
    
    cell.backgroundColor = [UIColor lightGrayColor];
    return cell;
    
}

-(BOOL)collectionView:(UICollectionView *)collectionView canMoveItemAtIndexPath:(NSIndexPath *)indexPath{
    
    return YES;
}

-(void)collectionView:(UICollectionView *)collectionView moveItemAtIndexPath:(nonnull NSIndexPath *)sourceIndexPath toIndexPath:(nonnull NSIndexPath *)destinationIndexPath{
    
    
    
}
#pragma mark 按键动作
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"%@",indexPath);
    
    [self.delegate didlikeClick:[self.arr objectAtIndex:indexPath.row]];
    
}


-(void)geturlwith:(NSString *)number{
    
    NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
    
    
    AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];
    
    NSURL *URL = [NSURL URLWithString:number];
    
    NSURLRequest *request = [NSURLRequest requestWithURL:URL];
    NSURLSessionDataTask *dataTask = [manager dataTaskWithRequest:request completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
        if (error) {
            NSLog(@"阿萨大大的");
        } else {
            self.arr = [NSMutableArray array];
            
            for (NSDictionary *dic in responseObject) {
                
                ModelOfLike *model = [[ModelOfLike alloc] init];
                [model setValuesForKeysWithDictionary:dic];
                
                model.idd = [NSString stringWithFormat:@"%@",[dic objectForKey:@"id"]];
                
                model.width = [NSString stringWithFormat:@"%@",[dic objectForKey:@"width"]];
                model.height = [NSString stringWithFormat:@"%@",[dic objectForKey:@"height"]];
                model.trip_id = [NSString stringWithFormat:@"%@",[dic objectForKey:@"trip_id"]];
                
                model.description1 = [dic objectForKey:@"description"];
                
                
                [self.arr addObject:model];
                
//                NSLog(@"%@",self.arr);
                
                [model release];
            }
            [self.collection reloadData];
        }
    }];
    [dataTask resume];
    

    
    
}


@end
